package com.mc.moring.view

import android.content.Context
import android.util.AttributeSet
import android.widget.EditText
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.widget.addTextChangedListener
import com.mc.moring.R

class InputVerifyCodeView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null
) : ConstraintLayout(context, attrs) {

    private var etCode1: EditText? = null
    private var etCode2: EditText? = null
    private var etCode3: EditText? = null
    private var etCode4: EditText? = null

    var verificationCodeCallBack: ((String) -> Unit)? = null
    var pointCallback: (() -> Unit)? = null

    init {
        inflate(context, R.layout.view_input_verify_code, this)
        etCode1 = findViewById(R.id.et_code_1)
        etCode2 = findViewById(R.id.et_code_2)
        etCode3 = findViewById(R.id.et_code_3)
        etCode4 = findViewById(R.id.et_code_4)
    }

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        etCode1?.setOnFocusChangeListener { v, hasFocus ->
            if(hasFocus) pointCallback?.invoke()
        }
        etCode1?.addTextChangedListener(afterTextChanged = {
            if ((it?.length ?: 0) >= 1) {
                etCode2?.requestFocus();
            }
        })
        etCode2?.addTextChangedListener(afterTextChanged = {
            if ((it?.length ?: 0) >= 1) {
                etCode3?.requestFocus();
            }
        })
        etCode3?.addTextChangedListener(afterTextChanged = {
            if ((it?.length ?: 0) >= 1) {
                etCode4?.requestFocus();
            }
        })
        etCode4?.addTextChangedListener(afterTextChanged = {
            if ((it?.length ?: 0) >= 1) {
                verifyCode()
            }
        })
        etCode1?.requestFocus()
    }

    fun onKeyDown():Boolean {
        if (etCode4?.isFocused == true) {
            if (etCode4?.text.isNullOrEmpty()) {
                etCode3?.text = null
                etCode3?.requestFocus()
            } else {
                etCode4?.text = null
            }
            return true
        }
        if (etCode3?.isFocused == true) {
            if (etCode3?.text.isNullOrEmpty()) {
                etCode2?.text = null
                etCode2?.requestFocus()
            } else {
                etCode3?.text = null
            }
            return true
        }

        if (etCode2?.isFocused == true) {
            if (etCode2?.text.isNullOrEmpty()) {
                etCode1?.text = null
                etCode1?.requestFocus()
            } else {
                etCode2?.text = null
            }
            return true
        }

        if (etCode1?.isFocused == true) {
            etCode1?.text = null
            return true
        }
        return false
    }

    private fun verifyCode() {
        val code = StringBuilder()
        etCode1?.extText()?.also { code.append(it) }
        etCode2?.extText()?.also { code.append(it) }
        etCode3?.extText()?.also { code.append(it) }
        etCode4?.extText()?.also { code.append(it) }
        verificationCodeCallBack?.invoke(code.toString())
        etCode1?.clearFocus()
        etCode2?.clearFocus()
        etCode3?.clearFocus()
        etCode4?.clearFocus()
    }

    private fun EditText.extText(): String? {
        return text.toString().takeIf { !it.isNullOrEmpty() }
    }

    fun cleanInput(){
        etCode1?.text = null
        etCode2?.text = null
        etCode3?.text = null
        etCode4?.text = null
        etCode1?.requestFocus();
    }

}